package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ImGroupEntivity;
import com.sam.im.heiqun.entities.SearchGroupEntity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.adapters.GroupAdapter;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.DividerItemDecorationUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查找群组
 */
public class SeachGroupActivity extends BaseSwipeBackActivity implements GroupAdapter.GroupListClickListener{

    //    @BindView(R.id.pre_v_back)
//    ImageView preVBack;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.friends_list)
    RecyclerView friendsList;
    @BindView(R.id.activity_seach_friends)
    LinearLayout activitySeachFriends;

    @BindView(R.id.relative_noresult)
    LinearLayout relative_noresult;
    @BindView(R.id.txt_clean)
    TextView txt_clean;

    private GroupAdapter mSearchFriendAdpter;

    PGService mPgService;
    List<ImGroupEntivity> groupEntivities;

    @Override
    public int getContentViewId() {
        return R.layout.activity_seach_friends;
    }

    @Override
    protected String getTitleText() {
        return getString(R.string.add_group);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
            mPgService = PGService.getInstance();

            mSearchFriendAdpter = new GroupAdapter(this);
            friendsList.setLayoutManager(new LinearLayoutManager(this));
            mSearchFriendAdpter.setItemClickListener(this);
            friendsList.addItemDecoration(new DividerItemDecorationUtils(this,LinearLayoutManager.HORIZONTAL,1,getResources().getColor(R.color.black_divider)));
            friendsList.setAdapter(mSearchFriendAdpter);
            groupEntivities = new ArrayList<>();
            searchEdit.setHint(R.string.input_mobile_ss);
            searchEdit.setOnKeyListener(onKeyListener);
            //接受传过来的值
            Intent intent=getIntent();
            String et_String_value=intent.getStringExtra("value");
            if (et_String_value!=null){
                queryGroup(et_String_value);
            }
            searchEdit.setText(et_String_value);

    }
    //    @OnClick(R.id.pre_v_back)
    @OnClick(R.id.txt_clean)
    public void onClick() {
        scrollToFinishActivity();
    }

    /**
     * 通过群名称查询群组列表
     */
    private void queryGroup(String name) {
        showProgress("");
        mPgService.queryGroup(ToolsUtils.getMyUserId(),name).subscribe(new AbsAPICallback<SearchGroupEntity>() {
            @Override
            public void onNext(SearchGroupEntity friendEntivity) {
//                showToast("好友申请已提交！");
                hideProgress();
                groupEntivities = friendEntivity.getInfo();
                mSearchFriendAdpter.refresh(groupEntivities);
//                scrollToFinishActivity();
                if(groupEntivities.size()==0){
                    showToast("没有该群，请查找其他的群");
                }
            }
            @Override
            protected void onResultError(ApiException ex) {
                hideProgress();
                Log.i("-----","--001ex.getDisplayMessage()--"+ex.getDisplayMessage());
            }
        });
    }

    @Override
    public void onGroupListClick(View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("groupSeri",groupEntivities.get(position));
        bundle.putLong("groupid", groupEntivities.get(position).getId());
        startActivity(GroupDetailsActivity.class,bundle);
    }

    private View.OnKeyListener onKeyListener = new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                String str = searchEdit.getText().toString().trim();
//                    Log.i("-----", "onEditorAction: str="+str);
                if (null != str && !"".equals(str.toString())) {
                    queryGroup(str.toString());
                }else {
//                        showToast("什么鬼啊");
                }
                return true;
            }
            return false;
        }
    };

}
